package com.cursordemo.controller;

import com.cursordemo.pojo.Emp;
import com.cursordemo.pojo.PageBean;
import com.cursordemo.pojo.Result;
import com.cursordemo.service.EmpService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;

/**
 * 员工管理Controller
 */
@Slf4j
@RestController
@RequestMapping("/emps")
public class EmpController {
    
    @Autowired
    private EmpService empService;
    
    /**
     * 员工列表查询
     */
    @GetMapping
    public Result<PageBean<Emp>> list(
            @RequestParam(defaultValue = "") String name,
            @RequestParam(required = false) Integer gender,
            @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
            @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end,
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer pageSize) {
        log.info("查询员工列表 - 参数: name={}, gender={}, begin={}, end={}, page={}, pageSize={}", 
                name, gender, begin, end, page, pageSize);
        
        PageBean<Emp> pageBean = empService.list(name, gender, begin, end, page, pageSize);
        
        log.info("查询员工列表完成 - 总记录数: {}, 当前页记录数: {}", 
                pageBean.getTotal(), pageBean.getRows().size());
        
        return Result.success(pageBean);
    }
    
    /**
     * 删除员工
     */
    @DeleteMapping
    public Result<Void> delete(@RequestParam List<Integer> ids) {
        log.info("删除员工 - 员工ID列表: {}", ids);
        
        empService.delete(ids);
        
        log.info("删除员工完成 - 成功删除 {} 个员工", ids.size());
        
        return Result.success();
    }
    
    /**
     * 添加员工
     */
    @PostMapping
    public Result<Void> add(@RequestBody Emp emp) {
        log.info("添加员工 - 员工信息: username={}, name={}, phone={}", 
                emp.getUsername(), emp.getName(), emp.getPhone());
        
        empService.add(emp);
        
        log.info("添加员工完成 - 员工ID: {}", emp.getId());
        
        return Result.success();
    }
    
    /**
     * 根据ID查询员工
     */
    @GetMapping("/{id}")
    public Result<Emp> getById(@PathVariable Integer id) {
        log.info("根据ID查询员工 - 员工ID: {}", id);
        
        Emp emp = empService.getById(id);
        
        if (emp != null) {
            log.info("查询员工成功 - 员工ID: {}, 姓名: {}", id, emp.getName());
        } else {
            log.warn("查询员工失败 - 员工ID: {} 不存在", id);
        }
        
        return Result.success(emp);
    }
    
    /**
     * 修改员工
     */
    @PutMapping
    public Result<Void> update(@RequestBody Emp emp) {
        log.info("修改员工 - 员工ID: {}, 姓名: {}, 用户名: {}", 
                emp.getId(), emp.getName(), emp.getUsername());
        
        empService.update(emp);
        
        log.info("修改员工完成 - 员工ID: {}", emp.getId());
        
        return Result.success();
    }
    
    /**
     * 查询全部员工
     */
    @GetMapping("/list")
    public Result<List<Emp>> listAll() {
        log.info("查询全部员工");
        
        List<Emp> empList = empService.listAll();
        
        log.info("查询全部员工完成 - 总记录数: {}", empList.size());
        
        return Result.success(empList);
    }
}
